//
//  ViewController.m
//  StickyDemo
//
//  Created by 胡不弍 on 2021/7/2.
//

#import "ViewController.h"
#import "StickyController.h"
#import "CustomViewController.h"

// 导航栏高度
#define NavBarHeight 44.0

// 头部宏
#define SafeAreaTopHeight (StatusBarHeight + NavBarHeight)
// 底部宏
#define SafeAreaBottomHeight 34

/** 屏幕宽 */
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
/** 屏幕高 */
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

static CGFloat animateDuration = 0.35;

@interface ViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) CustomViewController *sController;
@property (nonatomic, assign) BOOL isUP;
@property (nonatomic, assign) CGFloat pointY;
@property (nonatomic, assign) BOOL isAnimate;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.bgImageView];
    self.sController = [CustomViewController new];
    self.sController.view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self addChildViewController:self.sController];
    [self.view insertSubview:self.sController.view aboveSubview:self.scrollView];
    __weak typeof(self)weakSelf = self;
    [self.sController setDidDownREfresh:^{
        [weakSelf refresh];
    }];
    [self.sController setDidDownScroll:^(CGFloat offY) {
        [weakSelf didDownScroll:offY];
    }];
//    [self.sController setUnchangedScroll:^{
//        CGRect imageSize = [weakSelf imageLoadedFinish:weakSelf.bgImageView image:weakSelf.bgImageView.image skuInfo:nil];
//        CGFloat conMaxY = CGRectGetMaxY(weakSelf.bgImageView.frame);
//        CGFloat maxY = conMaxY / 2.0f + imageSize.size.height / 2.0f;
//        [UIView animateWithDuration:0.25 animations:^{
//            weakSelf.scrollView.frame = CGRectMake(0, - maxY  + 88, SCREEN_WIDTH, CGRectGetMaxY(weakSelf.bgImageView.frame));
//            weakSelf.sController.view.frame = CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT-88);
//        }];
//    }];
//    [self.sController setDidUpScroll:^(CGFloat offY) {
//        CGRect imageSize = [weakSelf imageLoadedFinish:weakSelf.bgImageView image:weakSelf.bgImageView.image skuInfo:nil];
//        CGFloat conMaxY = CGRectGetMaxY(weakSelf.bgImageView.frame);
//        CGFloat maxY = conMaxY / 2.0f + imageSize.size.height / 2.0f;
//        weakSelf.scrollView.frame = CGRectMake(0, - maxY  + 88 - offY, SCREEN_WIDTH, CGRectGetMaxY(weakSelf.bgImageView.frame));
//    }];
}
- (void)refresh  {
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:animateDuration animations:^{
        weakSelf.sController.view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
        weakSelf.scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
}
- (void)didDownScroll:(CGFloat)offY {
    
    CGRect imageSize = [self imageLoadedFinish:self.bgImageView image:self.bgImageView.image skuInfo:nil];
    CGFloat conMaxY = CGRectGetMaxY(self.bgImageView.frame);
    CGFloat maxY = conMaxY / 2.0f + imageSize.size.height / 2.0f;
    maxY -= 88;

    self.scrollView.frame = CGRectMake(0, - maxY - offY, SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    __weak typeof(self)weakSelf = self;
    if (y > 5 && !self.isAnimate) {
        self.isUP = YES;
        CGRect imageSize = [self imageLoadedFinish:self.bgImageView image:self.bgImageView.image skuInfo:nil];
        CGFloat conMaxY = CGRectGetMaxY(self.bgImageView.frame);
        CGFloat maxY = conMaxY / 2.0f + imageSize.size.height / 2.0f -88;
        [UIView animateWithDuration:animateDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            weakSelf.sController.view.frame = CGRectMake(0, maxY - y, SCREEN_WIDTH, SCREEN_HEIGHT);
        } completion:nil];
    }
    if (y <= 0 && !self.isAnimate) {
        self.isUP = NO;
        [UIView animateWithDuration:animateDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            weakSelf.sController.view.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
        } completion:nil];
        
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    CGFloat y = scrollView.contentOffset.y;
    CGRect imageSize = [self imageLoadedFinish:self.bgImageView image:self.bgImageView.image skuInfo:nil];
    CGFloat conMaxY = CGRectGetMaxY(self.bgImageView.frame);
    CGFloat maxY = conMaxY / 2.0f + imageSize.size.height / 2.0f;
    if (y > 5) {
        self.isAnimate = YES;
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:animateDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            scrollView.frame = CGRectMake(0, - maxY  + 88, SCREEN_WIDTH, CGRectGetMaxY(self.bgImageView.frame));
        } completion:nil];
        [UIView animateWithDuration:animateDuration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            weakSelf.sController.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        } completion:nil];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.isAnimate = NO;
}
#pragma mark - Method
- (CGRect)imageLoadedFinish:(UIImageView *)oldImageView image:(UIImage *)imageV skuInfo:(NSDictionary *)skuInfo {
    CGSize selfSize = self.view.frame.size;
    CGSize imageSize = imageV.size;
    CGRect imageVRect = self.bgImageView.frame;
    if(imageSize.width > selfSize.width) {
        imageVRect.size.height = selfSize.width * imageSize.height / imageSize.width;
        imageVRect.size.width = selfSize.width;
    }
    if(imageVRect.size.height > selfSize.height) {
        imageVRect.size.width = selfSize.height * imageVRect.size.width  / imageVRect.size.height;
        imageVRect.size.height = selfSize.height;
    }
//    imageVRect.origin.x = (selfSize.width-imageVRect.size.width)/2;
//    imageVRect.origin.y = -imageVRect.size.height + SafeAreaTopHeight + 60;
//    self.containerView.frame = imageVRect;
    
    return imageVRect;
}

#pragma mark - LazyLoad
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+1);
        _scrollView.delegate = self;
        if (@available(iOS 11.0, *)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [_scrollView setBounces:YES];
    }
    return _scrollView;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _bgImageView.image = [UIImage imageNamed:@"demo.jpg"];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _bgImageView;
}
@end
